package com.ktjiaoyu.controller;

import com.alibaba.fastjson2.JSON;
import com.ktjiaoyu.entity.Supplier;
import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.service.SysUserService;
import com.ktjiaoyu.utils.Constants;
import io.micrometer.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/main")
public class SysController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(SysController.class);

    @Autowired
    private SysUserService sysUserService;

    @ResponseBody
    @GetMapping("/oldPasswordCheck")
    public Map<String, Object> oldPasswordCheck(String oldPassword) throws Exception {

        SysUser sysUser = (SysUser) session(Constants.SESSION_USER);
        Map<String, Object> result = new HashMap<>();
        if (oldPassword == "" || oldPassword == null) {
            result.put("error", "输入不能为空,请重新输入");
        } else {
            try {
                boolean ok = sysUserService.validatePassword(sysUser.getId(), oldPassword);
                if (ok) {
                    result.put("pass", 1);
                } else {
                    result.put("pass", 0);
                }
            } catch (Exception e) {
                log.error("执行SysController.oldPasswordCheck(oldPassword)方法出现异常", e);
                throw new RuntimeException(e);
            }
        }
        return result;
    }

    @GetMapping("/changePwd")
    public String changePwd() {
        return "sys/changePwd";
    }

    //个人密码修改
    @PostMapping("/FchangePwd")
    public String FchangePwd(String password) throws Exception {
        SysUser sysUser = (SysUser) session(Constants.SESSION_USER);
        sysUser.setPassword(password);
        sysUser.setId(sysUser.getId());
        sysUser.setUpdatedTime(new Date());
        sysUser.setUpdatedUserId(sysUser.getId());
        boolean ok = sysUserService.updatePassword(sysUser.getId(), password);
        if (ok) {
            return redirect("/login.jsp");
        } else {
            return redirect("/main/changePwd");
        }
    }

//    @PostMapping("/changePwd")
//    public String upPassword(
//            @RequestParam("oldPassword") String oldPassword,
//            @RequestParam("password") String newPassword,
//            @RequestParam("confirm-password") String confirmPassword) {
//
//        HashMap<String, Object> result = new HashMap<>();
//        try {
//            // 1. 获取登录用户
//            SysUser loginUser = session(Constants.SESSION_USER);
//            boolean ok = sysUserService.updatePassword(loginUser.getId(), newPassword);
//            if (ok){
//                return redirect("/logout");
//            }
//        }catch (Exception e){
//            log.error("SysController中的upPassword方法执行发生错误", e);
//            throw new RuntimeException(e);
//        }
//        return "sys/changePwd";
//    }

}
